#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan Preference Package

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_33"
__date__    = "2013/12/08"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python

# esipc
from esipc import ESiPCProperty
from esipc.resource.iniresource import ESiPCINIFileResourceBundle

# Mikan
from mikan.core import MIKAN, AbstractMikan

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------
class MIKAN_PREFERENCE(MIKAN):
	ALL = 0
	SETTTING = 10
	SITE = 20
	GROUP = 30
	USER = 40

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

## Mikan Preference Abstract Class
#
class AbstractMikanPreference(AbstractMikan, MIKAN_PREFERENCE):

	def is_setting(self):
		return True

	# ----------------------------------------------------
	## リクエスト フィールドゲッター
	# @return 取得した値
	def get_request(self):
		return self.FRequest

	def set_value(self, name, value, level=MIKAN_PREFERENCE.USER):
		if level == MIKAN_PREFERENCE.USER:
			self.set_user_value(name, value)
		elif level == MIKAN_PREFERENCE.GROUP:
			self.set_group_value(name, value)
		elif level == MIKAN_PREFERENCE.SITE:
			self.set_site_value(name, value)

	def set_group_value(self, name, value):
		pass

	def set_site_value(self, name, value):
		pass

	def set_user_value(self, name, value):
		pass

	def get_value(self, name, default=None, level=MIKAN_PREFERENCE.ALL):
		r = None
		if level == MIKAN_PREFERENCE.USER:
			r = self.get_user_value(name)
		elif level == MIKAN_PREFERENCE.GROUP:
			r = self.get_group_value(name)
		elif level == MIKAN_PREFERENCE.SITE:
			r = self.get_site_value(name)
		elif level == MIKAN_PREFERENCE.SETTTING:
			if self.issetting:
				r = self.get_setting_value(name)
		elif level == MIKAN_PREFERENCE.ALL:
			r = self.get_user_value(name)
			if r:
				return r
			r = self.get_group_value(name)
			if r:
				return r
			r = self.get_site_value(name)
			if r:
				return r
			if self.issetting:
				r = self.get_setting_value(name)
				if r:
					return r
		if r:
			return r

		return default

	def get_group_value(self, name):
		return None

	def get_site_value(self, name):
		return None

	def get_user_value(self, name):
		return None

	def get_setting_value(self, name):
		return None

	# ----------------------------------------------------
	## constructor
	#  @param request request
	def __init__(self, request):
		super(AbstractMikanPreference, self).__init__()

		self.FRequest = request

	# ----------------------------------------------------
	## Property
	request = ESiPCProperty(get_request, None, None)
	issetting = ESiPCProperty(is_setting, None, None)

## Mikan INI Preference Class
#
class MikanINIPreference(AbstractMikanPreference):

	def get_ini(self):
		return self.FIni

	# ----------------------------------------------------
	## constructor
	#  @param request request
	def __init__(self, request):
		super(MikanINIPreference, self).__init__(request)

		config_name = request.mikanconfig + '/preference.ini'
		self.FIni = ESiPCINIFileResourceBundle(config_name)

	def get_setting_value(self, name):
		if self.request:
			if self.ini:
				try:
					return self.ini.get("preference", name)
				except:
					return None

	# ----------------------------------------------------
	## Property
	ini = ESiPCProperty(get_ini, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
